package servlet.goods;

import util.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet("/updategoods.do")
@MultipartConfig
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String goodsName = req.getParameter("goods_name");
        int type = Integer.parseInt(req.getParameter("type"));
        String goodsprice = req.getParameter("goods_price");
        String goodsdesc = req.getParameter("goods_desc");
        String supplier = req.getParameter("supplier");
        int  goods_id =Integer.parseInt(req.getParameter("goods_id"));
        int qty = Integer.parseInt(req.getParameter("num"));
        String goods_img = req.getParameter("goods_img");
        if (goods_img!=null){
            Part part = req.getPart("goods_img");
            String fileName = part.getSubmittedFileName();
            String path = "D:\\ideaproject\\shopping\\src\\main\\webapp\\images\\";
            path += fileName;
            goods_img = path;
            part.write(goods_img);
            DbUtil dbu = new DbUtil();
            String sql = "update goods set goods_name='"+goodsName+"',type_id="+type+",goods_price='"+goodsprice
                    +"',goods_desc='"+goodsdesc+"',supplier='"+supplier+"',qty="+qty+",goods_pic='"+goods_img+"' where goods_id="+goods_id+"";
            System.out.println(sql);
            if (dbu.executeUpdate(sql)<=0){

            }else {
                resp.sendRedirect("first_admin.jsp");
            }
        }
        DbUtil dbu = new DbUtil();
        String sql = "update goods set goods_name='"+goodsName+"',type_id="+type+",goods_price='"+goodsprice
                +"',goods_desc='"+goodsdesc+"',supplier='"+supplier+"',qty="+qty+" where goods_id="+goods_id+"";
        System.out.println(sql);
        if (dbu.executeUpdate(sql)<=0){

        }else {
            resp.sendRedirect("first_admin.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
